package applications.insurancecard.ui.lib;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import org.objectweb.fractal.fraclet.annotations.Component;
import org.objectweb.fractal.fraclet.annotations.Interface;
import org.objectweb.fractal.fraclet.annotations.Requires;

import applications.insurancecard.fc.api.Adress;

@Component(provides = {@Interface(name = "jPanelItf", signature = org.objectweb.fractal.swing.JPanelItf.class), @Interface(name = "jPanelAttributes", signature = org.objectweb.fractal.swing.JPanelAttributes.class), @Interface(name = "uicontainerservice", signature = ontocompo.api.UIContainerService.class), @Interface(name = "ontocompointrospection", signature = ontocompo.api.OntoCompoIntrospection.class)})
public class InformationAddress extends JPanel implements /*ContainerService,*/ org.objectweb.fractal.swing.JPanelItf, org.objectweb.fractal.swing.JPanelAttributes, ontocompo.api.UIContainerService, ontocompo.api.OntoCompoIntrospection {

	private String uid;
	private org.objectweb.fractal.api.Component c;
	
	//TODO
	@Requires(name = "addressstreetlabel")
	private ontocompo.api.UIContainerService addressstreetlabel;
	
	@Requires(name = "addressstreetvalue")
	private ontocompo.api.UIContainerService addressstreetvalue;
	
	@Requires(name = "addresszipcodelabel")
	private ontocompo.api.UIContainerService addresszipcodelabel;
	
	@Requires(name = "addresszipcodevalue")
	private ontocompo.api.UIContainerService addresszipcodevalue;
	
	@Requires(name = "addresscountrylabel")
	private ontocompo.api.UIContainerService addresscountrylabel;
	
	@Requires(name = "addresscountryvalue")
	private ontocompo.api.UIContainerService addresscountryvalue;
	//TODO
	
	public InformationAddress() {
		super();
		this.uid = "applications.insurancecard.ui.lib.InformationAddress";
		System.out.println(this.toString() + " created");
		//this.init();
	}
	
	public void init() {
		this.addressstreetlabel.init();
		this.addressstreetvalue.init();
		this.addresszipcodelabel.init();
		this.addresszipcodevalue.init();
		this.addresscountrylabel.init();
		this.addresscountryvalue.init();
		
		this.setBorder(BorderFactory.createLineBorder(Color.black, 1));
		this.setBackground(Color.white);

		this.setLayout(new GridLayout(1, 4));
		
		this.add(createLine(BoxLayout.Y_AXIS, 0, true, "Address", "", ""));
		
		JPanel detail = createBox(0);
		GridBagLayout dbl = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		detail.setLayout(dbl);
		c.gridx = 0;
		c.gridy = 0;
		c.anchor = GridBagConstraints.CENTER;
//		detail.add(createTh("Street"), c);
		detail.add(this.addressstreetlabel.getComponentItSelf(), c);
		c.gridx = 1;
		c.anchor = GridBagConstraints.WEST;
//		detail.add(createTd(" ### "), c);
		detail.add(this.addressstreetvalue.getComponentItSelf(), c);
		
		c.gridx = 0;
		c.gridy = 1;
		c.anchor = GridBagConstraints.CENTER;

//		detail.add(createTh("Zip Code"), c);
		detail.add(this.addresszipcodelabel.getComponentItSelf(), c);
		
		c.gridx = 1;
		c.anchor = GridBagConstraints.WEST;

//		detail.add(createTd(" ### "), c);
		detail.add(this.addresszipcodevalue.getComponentItSelf(), c);
		
		c.gridx = 0;
		c.gridy = 2;
		c.anchor = GridBagConstraints.CENTER;

//		detail.add(createTh("Country"), c);
		detail.add(this.addresscountrylabel.getComponentItSelf(), c);
		c.gridx = 1;	
		c.weightx = 1;
		c.anchor = GridBagConstraints.WEST;
//		detail.add(createTd(" ### "), c);
		detail.add(this.addresscountryvalue.getComponentItSelf(), c);
		
		this.add( detail);
		
		this.add(createLine(BoxLayout.Y_AXIS, 0, true, " ", " ", " "));
		this.add(createLine(BoxLayout.Y_AXIS, 0, true, " ", " ", " "));
	}
	
	protected JPanel createBox(int thickness)
	{
		JPanel result = new JPanel();
		if (thickness > 0) result.setBorder(BorderFactory.createLineBorder(Color.black, thickness));
		result.setBackground(Color.white);
		return result;
	}

	
	protected JLabel createTd(String txt)
	{
		JLabel result = new JLabel(txt);
		result.setFont(result.getFont().deriveFont(Font.PLAIN));
		result.setHorizontalAlignment(SwingConstants.LEFT);

		return result;
	}
//	
//	protected JLabel createTd(String txt, int thickness)
//	{
//		JLabel result = createTd(txt);
//		result.setBorder(BorderFactory.createLineBorder(Color.black, thickness));
//		return result;
//	}
//	
	protected JLabel createTh(String txt)
	{
		JLabel result = new JLabel(txt);
		// result.setBorder(BorderFactory.createLineBorder(Color.blue, 1));

		result.setHorizontalAlignment(SwingConstants.CENTER);
		return result;
	}
//	
//	protected JLabel createTh(String txt, int thickness)
//	{
//		JLabel result = createTh(txt);
//		result.setBorder(BorderFactory.createLineBorder(Color.black, thickness));
//		return result;
//	}
//	
	protected JLabel createTitle(String txt)
	{
		JLabel result = new JLabel(txt);
		result.setHorizontalAlignment(SwingConstants.LEFT);
		result.setBorder(BorderFactory.createLineBorder(Color.black, 1));
		return result;
	}
	
	protected JPanel createLine(int axis, int thickness, boolean title, String... msgs)
	{
		JPanel result = createBox(thickness);
		result.setLayout(new BoxLayout(result, axis));
		for(String s : msgs)
		{
			String msg = s;
			if (s == "") msg = " ";
			
			JLabel lbl ;
			if (title) lbl = createTh(msg);
			else lbl = createTd(msg);
			
			lbl.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
			lbl.setBorder(BorderFactory.createLineBorder(Color.black, thickness));
			
			result.add(lbl);
		}
		
		return result;
	}
//	
//	protected JPanel createLineLeftAlign(int axis, int thickness, boolean title, String... msgs)
//	{
//		JPanel result = createBox(thickness);
//		result.setLayout(new BoxLayout(result, axis));
//		for(String s : msgs)
//		{
//			String msg = s;
//			if (s == "") msg = " ";
//			
//			JLabel lbl ;
//			if (title) lbl = createTh(msg);
//			else lbl = createTd(msg);
//			
//			lbl.setAlignmentX(java.awt.Component.LEFT_ALIGNMENT);
//			lbl.setBorder(BorderFactory.createLineBorder(Color.black, thickness));
//			
//			result.add(lbl);
//		}
//		
//		return result;
//	}
	
//	@Override
	public JComponent getComponentItSelf() {
		// TODO Auto-generated method stub
		return this;
	}

	@Override
	public String getUid() {
		// TODO Auto-generated method stub
		return this.uid;
	}

	@Override
	public void setUid(String uid) {
		// TODO Auto-generated method stub
		this.uid = uid;
	}
	
	public String toString() {
		return "InformationAddress";
	}
	
	@Override
	public void update(Object o) {
		
		Adress adress = (Adress) o;
		
		this.addressstreetvalue.update(adress.getStreet());
		this.addresszipcodevalue.update(adress.getZipcode());
		this.addresscountryvalue.update(adress.getCountry());
		
		this.removeAll();
		
		this.setLayout(new GridLayout(1, 4));
		
		this.add(createLine(BoxLayout.Y_AXIS, 0, true, "Adress", "", ""));
		
		JPanel detail = createBox(0);
		GridBagLayout dbl = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		detail.setLayout(dbl);
		c.gridx = 0;
		c.gridy = 0;
		c.anchor = GridBagConstraints.CENTER;
//		detail.add(createTh("Street"), c);
		detail.add(this.addressstreetlabel.getComponentItSelf(), c);
		c.gridx = 1;
		c.anchor = GridBagConstraints.WEST;
//		detail.add(createTd(" ### "), c);
		detail.add(this.addressstreetvalue.getComponentItSelf(), c);
		
		c.gridx = 0;
		c.gridy = 1;
		c.anchor = GridBagConstraints.CENTER;

//		detail.add(createTh("Zip Code"), c);
		detail.add(this.addresszipcodelabel.getComponentItSelf(), c);
		
		c.gridx = 1;
		c.anchor = GridBagConstraints.WEST;

//		detail.add(createTd(" ### "), c);
		detail.add(this.addresszipcodevalue.getComponentItSelf(), c);
		
		c.gridx = 0;
		c.gridy = 2;
		c.anchor = GridBagConstraints.CENTER;

//		detail.add(createTh("Country"), c);
		detail.add(this.addresscountrylabel.getComponentItSelf(), c);
		c.gridx = 1;	
		c.weightx = 1;
		c.anchor = GridBagConstraints.WEST;
//		detail.add(createTd(" ### "), c);
		detail.add(this.addresscountryvalue.getComponentItSelf(), c);
		
		this.add( detail);
		
		this.add(createLine(BoxLayout.Y_AXIS, 0, true, " ", " ", " "));
		this.add(createLine(BoxLayout.Y_AXIS, 0, true, " ", " ", " "));
		
		this.getParent().validate();
	}

	@Override
	public org.objectweb.fractal.api.Component getFractalComponent() {
		// TODO Auto-generated method stub
		return this.c;
	}

	@Override
	public void setFractalComponent(org.objectweb.fractal.api.Component c) {
		// TODO Auto-generated method stub
		this.c = c;
	}

}